package com.hsmw.api.service;


import com.hsmw.api.config.jwt.CurrentHsmwUserUtils;
import com.hsmw.api.config.jwt.LoginUser;
import com.hsmw.api.mapper.HsmwGpsMapper;
import com.hsmw.api.mapper.HsmwHelpAvaliableInfoMapper;
import com.hsmw.api.mapper.HsmwHelpCenterMapper;
import com.hsmw.api.mapper.HsmwUserMapper;
import com.hsmw.common.base.BaseService;
import com.hsmw.domain.HsmwGps;
import com.hsmw.domain.HsmwHelpAvaliableInfo;
import com.hsmw.domain.HsmwHelpCenter;
import com.htn.common.core.base.DataResponse;
import com.htn.common.core.constant.CommonConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import static com.htn.common.core.constant.CommonConstants.ResponseStatus.FAIL;

@Service
@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class HsmwHelpCenterService   extends BaseService<HsmwHelpCenterMapper, HsmwHelpCenter> {


    @Autowired
    private HsmwHelpCenterMapper hsmwHelpCenterMapper;

    @Autowired
    private HsmwHelpAvaliableInfoMapper hsmwHelpAvaliableInfoMapper;

    @Autowired
    private CurrentHsmwUserUtils currentHsmwUserUtils;

    public DataResponse updateHelpCenterNum(String id, String params) {
            LoginUser user = currentHsmwUserUtils.getCurrentUserInfo();
            if(com.htn.common.core.util.StringUtils.isBlank(params)){
                return new DataResponse(FAIL,"传递参数错误");
            }
            HsmwHelpCenter hsmwHelpCenter = hsmwHelpCenterMapper.selectById(id);
            HsmwHelpAvaliableInfo info = new HsmwHelpAvaliableInfo();
            if(com.htn.common.core.util.StringUtils.equals("1",params)){
                info.setAvailableFlag(CommonConstants.YES);
                info.setUserId(user.getId());
                info.setHelpId(id);
                hsmwHelpAvaliableInfoMapper.insert(info);
            }else if(com.htn.common.core.util.StringUtils.equals("2",params)) {
                info.setAvailableFlag(CommonConstants.NO);
                info.setUserId(user.getId());
                info.setHelpId(id);
                hsmwHelpAvaliableInfoMapper.insert(info);
            }else {
                hsmwHelpCenter.setClickCount(hsmwHelpCenter.getClickCount() + 1);
            }
            hsmwHelpCenterMapper.updateById(hsmwHelpCenter);
            return new DataResponse();
        }

}
